package cn.openea.perpartner.meily.web.mapper;

import cn.openea.perpartner.meily.web.entity.GroupBuying;
import cn.openea.perpartner.meily.web.entity.GroupBuyingSub;
import cn.openea.perpartner.meily.web.entity.dto.GroupBuyingDTOFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * (GroupBuying)表数据库访问层
 *
 * @author rains
 * @date 2023/2/7
 */
@Repository
public interface GroupBuyingMapper extends BaseMapper<GroupBuying> {

    /**
     * 获取我发布的团购列表
     * @param wrapper
     * @return
     */
    @Select("select * from meily_group_buying where 1=1 ${ew.customSqlSegment}")
    List<GroupBuying> getPublishGroupPurchaseList(@Param("ew") QueryWrapper<GroupBuying> wrapper);

    /**
     * 获取我参与的团购列表
     * @param wrapper
     * @return
     */
    @Select("select a.* from meily_group_buying a left join meily_group_buying_sub b on a.id = b.main_docunid where 1=1 ${ew.customSqlSegment}")
    List<GroupBuying> getJoinGroupPurchaseList(@Param("ew") QueryWrapper<GroupBuying> wrapper);

    /**
     * 获取我参与的团购总数
     * @param wrapper
     * @return
     */
    @Select("select count(1) from meily_group_buying a left join meily_group_buying_sub b on a.id = b.main_docunid where 1=1 ${ew.customSqlSegment}")
    Long getJoinGroupPurchaseNum(@Param("ew") QueryWrapper<GroupBuying> wrapper);

    /**
     * 获取已到期需要结算的拼团记录
     * 说明：精确到秒
     * @return
     */
    @Select("select * from meily_group_buying where `status` = '1' HAVING timestampdiff(SECOND, now(), DATE_FORMAT(end_time, '%Y-%m-%d %H:%m:%s')) <= 0 ORDER BY end_time")
    List<GroupBuying> getBecomeDueGroupList();

    /**
     * 获取商品销量大于100的拼团记录（拼团未结束）
     * @return
     */
    @Select("select * from meily_group_buying where `status` = '1' and id in (select main_docunid from meily_group_buying_sub ${ew.customSqlSegment})")
    List<GroupBuying> getlist(@Param("ew") QueryWrapper<GroupBuying> wrapper);

    /**
     * 获取当前月已结束的拼团记录
     * @return
     */
    @Select("select * from meily_group_buying where DATE_FORMAT(finish_time, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m') and status = '2'")
    List<GroupBuying> getCurrentMonthFinishList();

    /**
     * 获取上个月已结束的拼团记录
     * @return
     */
    @Select("select * from meily_group_buying where DATE_FORMAT(finish_time, '%Y%m') = DATE_FORMAT(date_sub(CURDATE(), interval 1 month), '%Y%m') and status = '2'")
    List<GroupBuying> getLastMonthFinishList();

    /**
     * 获取指定发起人的团购销售数据
     * @param initiatorId 发起人编号
     * @return map
     */
    @Select("SELECT COUNT(DISTINCT goods_no) AS agentGoodsQuantity, SUM(used_quantity * distribution_reward) AS agentSalesRevenue\n" +
            "FROM meily_group_buying\n" +
            "WHERE initiator_id = #{initiatorId}")
    GroupBuyingDTOFactory.GroupBuyingStatisticsDto groupBuyingStatistics(String initiatorId);
}
